package rpc

import (
	"fmt"
	"github.com/zeromicro/go-zero/core/mr"
	"shangke-device/app/svcContent"
	"shangke-device/internal/work"
	"shangke-device/internal/ymqtt"
)

type Controller struct {
	svc  *svcContent.SvcContent
	work work.Work
}

type PublishRequest struct {
	Topic    string `json:"topic"`
	Qos      byte   `json:"qos"`
	Retained bool   `json:"retained"`
	Data     ymqtt.RequesterData
}

type PublishResponse struct {
	Code   int
	Msg    string
	Result bool
}

func (c Controller) Publish(req *PublishRequest, res *PublishResponse) error {
	fmt.Printf("publis: %+v \r\n", req)

	r, err := c.svc.Client.Publish(req.Topic, req.Qos, req.Retained, req.Data)

	res.Code = 200
	if err != nil {
		res.Msg = err.Error()
	} else {
		res.Msg = "成功"
		res.Result = r
	}
	return nil
}

type OpenRequest struct {
	Topic    string
	Qos      byte
	Retained bool
	Data     ymqtt.RequesterData
}

type OpenResponse struct {
	Code   int
	Msg    string
	Result bool
}

func (c Controller) Open() error {
	return mr.Finish(func() (err error) {
		userInfo, err = getUser()
		return err
	}, func() (err error) {
		productList, err = getProductList()
		return err
	}, func() (err error) {
		productList, err = getProductList()
		return err
	})
}

func NewController(svc *svcContent.SvcContent) *Controller {
	return &Controller{svc: svc, work: work.NewWork()}
}
